package com.xunwulian.web.bean;

import java.sql.Timestamp;
import java.util.Date;
import java.util.Map;

/**
 * 微信
 * 回调时返回的数据包装的类
 */
public class WeixinCallback {
    // 公众账号
    public String appid;

    // 商家数据包
    public String attach;

    // 付款银行
    public String bank_type;

    // 现金支付金额
    public int cash_fee;

    // 货币种类
    public String fee_type;

    // 是否关注公众账号
    public boolean is_subscribe;

    // 商户号
    public String mch_id;

    // 随机字符串
    public String nonce_str;

    // 用户标识
    public String openid;

    // 商户订单号
    public String out_trade_no;

    // 业务结果
    public boolean result_code;

    // 返回状态码
    public boolean return_code;

    // 签名
    public String sign;

    // 支付完成时间
    public Date time_end;

    // 订单金额
    public int total_fee;

    // 交易类型
    public String trade_type;

    // 微信支付订单号
    public String transaction_id;

    /**
     * 根据返回的xml数据转换的map构造一个实例
     *
     * @param paramMap 回调的xml数据转换的map
     */
    public WeixinCallback(Map<String, String> paramMap) {
        // 设置返回码
        if (paramMap.containsKey("return_code")) {
            return_code = paramMap.get("return_code").equals("PAID");
        } else {
            return_code = false;
            result_code = false;
        }

        // 设置结果码
        if (paramMap.containsKey("result_code")) {
            result_code = paramMap.get("result_code").equals("PAID");
        } else {
            result_code = false;
        }

        appid = paramMap.get("appid");
        attach = paramMap.get("attach");
        bank_type = paramMap.get("bank_type");
        fee_type = paramMap.get("fee_type");
        mch_id = paramMap.get("mch_id");
        nonce_str = paramMap.get("nonce_str");
        openid = paramMap.get("openid");
        out_trade_no = paramMap.get("out_trade_no");
        sign = paramMap.get("sign");
        trade_type = paramMap.get("trade_type");
        transaction_id = paramMap.get("transaction_id");

        if (paramMap.containsKey("cash_fee")) {
            cash_fee = Integer.parseInt(paramMap.get("cash_fee"));
        }
        if (paramMap.containsKey("is_subscribe")) {
            is_subscribe = paramMap.get("is_subscribe").equals("Y");
        }
        if (paramMap.containsKey("time_end")) {
            String time = paramMap.get("time_end");
            time_end = Timestamp.valueOf(time.substring(0, 4) + "-" + time.substring(4, 6) + "-" + time.substring(6, 8)
                    + " " + time.substring(8, 10) + ":" + time.substring(10, 12) + ":" + time.substring(12, 14));
        }
        if (paramMap.containsKey("total_fee")) {
            total_fee = Integer.parseInt(paramMap.get("total_fee"));
        }
    }

    @Override
    public String toString() {
        StringBuilder inf = new StringBuilder();
        inf.append("[公众账号=" + appid + "]\n");
        inf.append("[商家数据包=" + attach + "]\n");
        inf.append("[付款银行=" + bank_type + "]\n");
        inf.append("[现金支付金额=" + cash_fee + "]\n");
        inf.append("[支付种类=" + fee_type + "]\n");
        inf.append("[是否关注公众账号=" + is_subscribe + "]\n");
        inf.append("[商户号=" + mch_id + "]\n");
        inf.append("[随机字符串=" + nonce_str + "]\n");
        inf.append("[用户标示=" + openid + "]\n");
        inf.append("[商户订单号=" + out_trade_no + "]\n");
        inf.append("[业务结果=" + result_code + "]\n");
        inf.append("[返回状态码=" + return_code + "]\n");
        inf.append("[签名=" + sign + "]\n");
        inf.append("[支付完成时间=" + time_end + "]\n");
        inf.append("[订单金额=" + total_fee + "]\n");
        inf.append("[交易类型=" + trade_type + "]\n");
        inf.append("[微信支付订单号=" + transaction_id + "]\n");
        return inf.toString();
    }
}
